package com.teamelite.exmgmt;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class cListAktivitasDetail extends Activity implements ViewFactory,View.OnClickListener {
	ArrayList<arrList> lstTips,lstDoActivity;
	int tips_count,last_tipsid;
	String id_activitydo;
	TextSwitcher txtInfo;
	Animation in,out;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detailactivity);
		
		DBHelper db = new DBHelper(this);
		
		this.tips_count = db.getCountTips();
		this.last_tipsid = -1;
		
		this.id_activitydo = getIntent().getExtras().getString("id_activitydo");
		
		lstTips = db.getTips();
		lstDoActivity = db.getActivityByIdActivityDo(this.id_activitydo);
		
		// Set Ke Overview
		((TextView)findViewById(R.id.tvDurationTP)).setText("Activity Duration");
		((TextView)findViewById(R.id.txtActivityName)).setText(""+lstDoActivity.get(0).nama_activity);
		((TextView)findViewById(R.id.txtDuration)).setText(""+lstDoActivity.get(0).durationtodo);
		((TextView)findViewById(R.id.txtRestDuration)).setText(""+lstDoActivity.get(0).timestodo);
		((TextView)findViewById(R.id.txtCalories)).setText(String.format("%.2f cal", Double.parseDouble(lstDoActivity.get(0).calory)));
		
		// Set Listener Tombol
		((ImageView)findViewById(R.id.btnBack)).setOnClickListener(this);
		
		txtInfo = (TextSwitcher) findViewById(R.id.txtInfo);
		txtInfo.setFactory(cListAktivitasDetail.this);
        in = AnimationUtils.loadAnimation(cListAktivitasDetail.this, R.anim.push_up_in);
        out = AnimationUtils.loadAnimation(cListAktivitasDetail.this, R.anim.push_up_out);
        
		Timer timer = new Timer();
		timer.schedule(new tTask(), 0,10000);
	}
	
	class tTask extends TimerTask {
	     @Override
	     public void run() {
	         cListAktivitasDetail.this.runOnUiThread(new Runnable() {
	             @Override
	             public void run() {
	            	 Random generate = new Random();
	            	 int r;
	                 txtInfo.setInAnimation(in);
	                 txtInfo.setOutAnimation(out);
	                 do{
	                	 r = generate.nextInt(tips_count);
	                 }while(r==last_tipsid);
	                 last_tipsid = r;
	                 txtInfo.setText(lstTips.get(r).tips);
	             }
	         });
	     }
	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.LEFT);
        t.setTextSize(14);
        return t;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.btnBack:
				finish();
			break;
		}
	};
	
}
